package com.qlslylq.ad.sdk.util;

import com.qlslylq.ad.sdk.application.AdMergeSDKApp;
import com.qlslylq.ad.sdk.constant.AppConst;

import java.io.Serializable;

/**
 * created by qlslylq <br/>
 * on 2014 <br/>
 * Log统一管理类<br/>
 */
public class Log {

    /**
     * 默认的输出标签
     */
    public static final String TAG = "AdMergeSDK";

    /**
     * 是否使用详细的网络请求调用栈，用于查找网络请求链路，默认false，输出链路上接近请求管理的三个点<br/>
     * 切换为true则输出链路上的所有点<br/>
     */
    public static boolean exactStackTrace = false;

    /**
     * 单行最大输出长度
     */
    private static final int SINGLELINE_MAX_LENGTH = 1000;

    private Log() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void v(Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogVerbose(TAG, msg);
        }
    }

    public static void d(Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogDebug(TAG, msg);
        }
    }

    public static void i(Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogInfo(TAG, msg);
        }
    }

    public static void w(Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogWarn(TAG, msg);
        }
    }

    public static void e(Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogError(TAG, msg);
        }
    }

    public static void v(Object tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            String suffix = "";
            if (!(tag instanceof String)) {
                suffix = "：" + getCodeLine(tag.getClass(), StackTraceUtils.getCodeLine());
                recordLogVerbose(TAG, msg + suffix);
            } else {
                suffix = "：" + StackTraceUtils.getActivityStackTraceElement();
                recordLogVerbose(tag + "", msg + suffix);
            }
        }
    }

    public static void d(Object tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            String suffix = "";
            if (!(tag instanceof String)) {
                suffix = "：" + getCodeLine(tag.getClass(), StackTraceUtils.getCodeLine());
                recordLogDebug(TAG, msg + suffix);
            } else {
                suffix = "：" + StackTraceUtils.getActivityStackTraceElement();
                recordLogDebug(tag + "", msg + suffix);
            }
        }
    }

    public static void ds(String tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogDebug(tag, msg);
        }
    }

    /**
     * 流程化日志(私密)<br/>
     */
    public static void dp(Object msg) {
        if (AppConst.openSecretLog) {
            recordLogDebug(TAG, msg);
        }
    }

    /**
     * 流程化日志(私密)<br/>
     * json化<br/>
     */
    public static void dpj(Serializable ser) {
        if (AppConst.openSecretLog) {
            dp(GsonUtils.gsonString(ser));
        }
    }

    public static void i(Object tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            String suffix = "";
            if (!(tag instanceof String)) {
                suffix = "：" + getCodeLine(tag.getClass(), StackTraceUtils.getCodeLine());
                recordLogInfo(TAG, msg + suffix);
            } else {
                suffix = "：" + StackTraceUtils.getActivityStackTraceElement();
                recordLogInfo(tag + "", msg + suffix);
            }
        }
    }

    public static void w(Object tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            String suffix = "";
            if (!(tag instanceof String)) {
                suffix = "：" + getCodeLine(tag.getClass(), StackTraceUtils.getCodeLine());
                recordLogWarn(TAG, msg + suffix);
            } else {
                suffix = "：" + StackTraceUtils.getActivityStackTraceElement();
                recordLogWarn(tag + "", msg + suffix);
            }
        }
    }

    public static void ws(String tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogWarn(tag, msg);
        }
    }

    public static void e(Object tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            String suffix = "";
            if (!(tag instanceof String)) {
                suffix = "：" + getCodeLine(tag.getClass(), StackTraceUtils.getCodeLine());
                recordLogError(TAG, msg + suffix);
            } else {
                suffix = "：" + StackTraceUtils.getActivityStackTraceElement();
                recordLogError(tag + "", msg + suffix);
            }
        }
    }

    public static void es(String tag, Object msg) {
        if (AdMergeSDKApp.isDebug()) {
            recordLogError(tag, msg);
        }
    }

    /**
     * 流程化日志(私密)<br/>
     */
    public static void ep(Object msg) {
        if (AppConst.openSecretLog) {
            recordLogError(TAG, msg);
        }
    }

    private static void recordLogVerbose(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        String result = "\n" + msg.toString().replace("<br/>", "\n") + "\n";
        android.util.Log.v(tag, result);
    }

    private static void recordLogDebug(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        String result = "\n" + msg.toString().replace("<br/>", "\n") + "\n";
        android.util.Log.d(tag, result);
    }

    private static void recordLogInfo(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        String result = "\n" + msg.toString().replace("<br/>", "\n") + "\n";
        android.util.Log.i(tag, result);
    }

    private static void recordLogWarn(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        String result = "\n" + msg.toString().replace("<br/>", "\n") + "\n";
        android.util.Log.w(tag, result);
    }

    private static void recordLogError(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        String result = "\n" + msg.toString().replace("<br/>", "\n") + "\n";
        android.util.Log.e(tag, result);
    }

    /**
     * 输出长文本 <br/>
     * 正则过滤前缀：.*接口数据：.*： <br/>
     * idea.cycle.buffer.size=1024000  <br/>
     */
    public static void longText(String tag, String prefix, String msg) {
        int start = 0;
        int end = SINGLELINE_MAX_LENGTH;
        int msg_difference = msg.length() - SINGLELINE_MAX_LENGTH;
        int index = 0;
        if (msg_difference > 0) {
            for (; ; ) {
                Log.d(tag, prefix + "{" + index++ + "}：" + msg.substring(start, end));
                start = end;
                end += SINGLELINE_MAX_LENGTH;
                if (end >= msg.length()) {
                    Log.d(tag, prefix + "{" + index + "}：" + msg.substring(start));
                    return;
                }
            }
        } else {
            Log.d(tag, prefix + msg);
        }
    }

    public static String getCodeLine(Object obj) {
        return "at " + obj.getClass().getName() + "(" + obj.getClass().getSimpleName() + ".java:1)";
    }

    public static String getCodeLine(Object obj, int line) {
        return "at " + obj.getClass().getName() + "(" + obj.getClass().getSimpleName() + ".java:" + line + ")";
    }

    public static String getCodeLine(Class target, int line) {
        return "at " + target.getName() + "(" + target.getSimpleName() + ".java:" + line + ")";
    }

}
